import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class Sort {

    /**
     * 快速排序-》
     * 时间复杂度：
     *       最好的情况下：O(N*logN)
     *       最坏的情况下：O(N^2) 逆序/有序
     * 空间复杂度：
     *       N*logN
     * 稳定性：不稳定
     */
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    public static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int partitionHoare(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return left;
    }

    /**
     * 挖坑法
     */
    public static int partitionHole(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
            swap(array,left,right);
        }
        array[left] = tmp;
        return left;
    }

    /**
     * 前后指针法
     * 总结：
     * 1.Hoare
     * 2.挖坑法
     * 3.前后指针法
     * 这3种方式 每次划分后的前后顺序 有可能是不一样的
     */
    public static int partition(int[] array, int left, int right) {
        int prev = left;
        int cut = left+1;
        while (cut <= right) {
            if (array[cut] < array[left] && array[++prev] != array[cut]) {
                swap(array,cut,prev);
            }
            cut++;
        }
        swap(array,prev,left);
        return prev;
    }

    //快速排序非递归
    public static void quickSortNor(int[] array) {
        int start = 0;
        int end = array.length-1;
        Stack<Integer> stack = new Stack<>();
        int pivot = partitionHoare(array,start,end);
        if (pivot > start+1) {
            stack.push(start);
            stack.push(pivot-1);
        }
        if (pivot < end-1) {
            stack.push(pivot+1);
            stack.push(end);
        }
        while (!stack.isEmpty()) {
            end = stack.pop();
            start = stack.pop();
            pivot = partitionHoare(array,start,end);
            if (pivot > start+1) {
                stack.push(start);
                stack.push(pivot-1);
            }
            if (pivot < end-1) {
                stack.push(pivot+1);
                stack.push(end);
            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    //插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
    }

    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j-=gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    /**
     * 归并排序-》
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(logN)
     * 稳定性：稳定的排序
     * 目前为止3个稳定的排序：直接插入排序、冒泡排序、归并排序
     */
    public static void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length-1);
    }

    private static void mergeSortFun(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int mid = (start+end)/2;
        mergeSortFun(array,start,mid);
        mergeSortFun(array,mid+1,end);
        merge(array,start,mid,end);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        int[] tmpArr = new int[right-left+1];
        int k = 0;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        for (int i = 0; i < tmpArr.length; i++) {
            array[i+left] = tmpArr[i];
        }
    }

    /**
     * 非递归实现归并排序
     */
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i+=2*gap) {
                int left = i;
                int mid = left+gap-1;
                int right = mid+gap;
                if (mid >= array.length) {
                    mid = array.length-1;
                }
                if (right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    /**
     * 计数排序的场景：
     * 指定范围内的数据
     * 时间复杂度：O(MAX(N+范围))
     * 空间复杂度：O(范围)
     * 稳定性：稳定的排序(目前所写代码不稳定)
     */
    public static void countSort(int[] array) {
        int minVal = array[0];
        int maxVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if (minVal > array[i]) {
                minVal = array[i];
            }
            if (maxVal < array[i]) {
                maxVal = array[i];
            }
        }
        int len = maxVal - minVal + 1;
        int[] count = new int [len];
        for (int i = 0; i < array.length; i++) {
            count[array[i]-minVal]++;
        }
        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index] = i+minVal;
                index++;
                count[i]--;
            }
        }
    }
}